﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using VSLangProj;

namespace Planet.TransformDslExt
{
    internal static class VSHelper
    {
        public static IVsHierarchy GetSelectedProject(IServiceProvider serviceProvider)
        {
            IVsMonitorSelection monitorSelection = serviceProvider.GetService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;
            IntPtr hierarchyPtr;
            uint itemid;
            IVsMultiItemSelect multiItemSelect;
            IntPtr selectionContainer;
            monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainer);
            
            return Marshal.GetObjectForIUnknown(hierarchyPtr) as IVsHierarchy;
        }

        public static EnvDTE.Project ToDteProject(IVsHierarchy hierarchy)
        {
            if (hierarchy == null) return null;

            object obj2 = null;
            if (hierarchy.GetProperty(0xfffffffe, -2027, out obj2) < 0)
            {
                throw new ArgumentException("Hierarchy is not a project", "hierarchy");
            }

            return (EnvDTE.Project)obj2;
        }

        public static void FindProjectItems(Project project, List<ProjectItem> toProcess)
        {
            if (((project.Kind == "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") || (project.Kind == "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}")) && (project.ProjectItems != null))
            {
                foreach (ProjectItem item in project.ProjectItems)
                {
                    FindProjectItemDependents(item, toProcess);
                }
            }
            else if ((project.Object is SolutionFolder) && (project.ProjectItems != null))
            {
                foreach (ProjectItem item2 in project.ProjectItems)
                {
                    Project project2 = item2.Object as Project;
                    if (project2 != null)
                    {
                        FindProjectItems(project2, toProcess);
                    }
                }
            }
        }

        private static void FindProjectItemDependents(ProjectItem item, List<ProjectItem> toProcess)
        {
            VSProjectItem item2 = item.Object as VSProjectItem;
            bool flag = false;
            if (item2 != null)
            {
                string customTool = GetCustomTool(item);
                if ((customTool != null) && (StringComparer.OrdinalIgnoreCase.Compare(customTool, "TextTemplatingFileGenerator") == 0))
                {
                    flag = true;
                }
            }
            if (item.ProjectItems != null)
            {
                foreach (ProjectItem item3 in item.ProjectItems)
                {
                    FindProjectItemDependents(item3, toProcess);
                }
            }
            if (flag)
            {
                toProcess.Add(item);
            }
        }

        public static string GetCustomTool(ProjectItem item)
        {
            if (item.Properties != null)
            {
                Property property = null;
                try
                {
                    property = item.Properties.Item("CustomTool");
                }
                catch (ArgumentException)
                {
                }
                if (property != null)
                {
                    string str = property.Value as string;
                    if (!string.IsNullOrEmpty(str))
                    {
                        return str;
                    }
                }
            }
            return null;
        }

 

 


 

    }
}
